package com.example.tetris.ui.GameBodyScreen

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment.Companion.BottomCenter
import androidx.compose.ui.Alignment.Companion.Center
import androidx.compose.ui.Alignment.Companion.CenterEnd
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Alignment.Companion.CenterStart
import androidx.compose.ui.Alignment.Companion.TopCenter
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.tetris.R
import com.example.tetris.logic.DirectionButtonSize
import com.example.tetris.logic.RotateButtonSize
import com.example.tetris.logic.SettingButtonSize
import com.example.tetris.logic.model.Clickable
import com.example.tetris.logic.model.Direction
import com.example.tetris.logic.model.combinedClickable
import com.example.tetris.ui.theme.BodyColor
import com.example.tetris.ui.theme.ScreenBackground


//val ButtonText = @Composable { modifier: Modifier,
//                               text: String ->
//    Text(
//        text, modifier = modifier,
//        color = Color.White.copy(0.9f),
//        fontSize = 18.sp
//    )
//}


//@Preview
//@Composable
//fun GameBodyScreen(clickable: Clickable? = null) {
//    val DirectionButtonSize = 40.dp
//
//    Column(
//        Modifier
//            .height(500.dp)
//            .fillMaxWidth()
//    ) {
//        Row(Modifier.weight(3f)) {
//            Button(onClick = { /*TODO*/ }, modifier = Modifier.weight(1f)) {
//                Text(text = "sounds")
//            }
//            Button(onClick = { /*TODO*/ }, modifier = Modifier.weight(1f)) {
//                Text(text = "pause/resume")
//            }
//            Button(onClick = { /*TODO*/ }, modifier = Modifier.weight(1f)) {
//                Text(text = "start/reset")
//            }
//        }
//        Row(Modifier.weight(7f)) {
//            Box(
//                modifier = Modifier
//                    .height(100.dp)
//                    .weight(1f)
//            ) {
//                GameButton(
//                    size = DirectionButtonSize,
//                    modifier = Modifier.align(Alignment.TopCenter),
//                    onClick = {
//                        clickable?.onMove?.invoke(Direction.Up)
//                    }) {
//                    ButtonText(modifier = it, painterResource(id = R.drawable.up))
//                }
//                GameButton(
//                    size = DirectionButtonSize,
//                    modifier = Modifier.align(Alignment.CenterStart),
//                    onClick = { clickable?.onMove?.invoke(Direction.Left) }) {
//                    ButtonText(modifier = it, painterResource(id = R.drawable.back))
//                }
//                GameButton(
//                    size = DirectionButtonSize,
//                    modifier = Modifier.align(Alignment.CenterEnd),
//                    onClick = { clickable?.onMove?.invoke(Direction.Right) }) {
//                    ButtonText(modifier = it, painterResource(id = R.drawable.right_arrow))
//                }
//                GameButton(
//                    size = DirectionButtonSize,
//                    modifier = Modifier.align(Alignment.BottomCenter),
//                    onClick = { clickable?.onMove?.invoke(Direction.Down) }) {
//                    ButtonText(modifier = it, painterResource(id = R.drawable.down))
//                }
//            }
//            GameButton(size = 80.dp, onClick = { /*TODO*/ }, modifier = Modifier.weight(1f)) {
//                ButtonText(modifier = it, painterResource(id = R.drawable.rotate_cw))
//            }
//        }
//    }
//}

@Composable
fun GameBody(clickable: Clickable = combinedClickable(), screen: @Composable () -> Unit) {
    Column(
        Modifier
            .fillMaxSize()
            .background(Color.Black)
            .background(BodyColor, RoundedCornerShape(10.dp))
            .padding(20.dp)
    ) {
        Box(modifier = Modifier.align(CenterHorizontally)) {
            Box(
                modifier = Modifier
                    .align(Center)
                    .size(330.dp, 400.dp)
                    .padding(top = 20.dp)
                    .background(Color.Black.copy(alpha = 0.8f))
                    .padding(5.dp)
                    .background(BodyColor)
            )

            Box(
                modifier = Modifier
                    .width(120.dp)
                    .height(45.dp)
                    .align(TopCenter)
                    .background(BodyColor)
            ) {
                Text(
                    text = stringResource(id = R.string.body_label),
                    fontFamily = FontFamily.Cursive,
                    modifier = Modifier.align(
                        Center
                    ),
                    textAlign = TextAlign.Center,
                    fontWeight = FontWeight.Bold,
                    fontSize = 20.sp
                )

            }


            Box(
                modifier = Modifier
                    .align(Center)
                    .size(360.dp, 380.dp)
                    .padding(start = 50.dp, end = 50.dp, top = 50.dp, bottom = 30.dp)
            ) {
                Canvas(modifier = Modifier.fillMaxSize()) {
                    drawScreenBorder(
                        Offset(0f, 0f),
                        Offset(size.width, 0f),
                        Offset(0f, size.height),
                        Offset(
                            size.width, size.height
                        )
                    )
                }
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(6.dp)
                        .background(ScreenBackground)
                ) {
                    screen()
                }
            }
        }
        Spacer(modifier = Modifier.height(20.dp))

        val SettingText = @Composable { text: String, modifier: Modifier ->
            Text(
                text = text,
                modifier = modifier,
                color = Color.Black.copy(0.9f),
                fontSize = 12.sp,
                textAlign = TextAlign.Center
            )
        }

        Column(Modifier.padding(start = 40.dp, end = 40.dp)) {
            Row() {
                SettingText(stringResource(id = R.string.button_sounds), Modifier.weight(1f))
                SettingText(stringResource(id = R.string.button_pause), Modifier.weight(1f))
                SettingText(stringResource(id = R.string.button_reset), Modifier.weight(1f))
            }
            Spacer(modifier = Modifier.height(5.dp))
            Row() {
                GameButton(
                    size = SettingButtonSize, onClick = { clickable.onMute() },
                    modifier = Modifier
                        .weight(1f)
                        .padding(start = 20.dp, end = 20.dp)
                ) {}

                GameButton(
                    size = SettingButtonSize, onClick = { clickable.onPause() },
                    modifier = Modifier
                        .weight(1f)
                        .padding(start = 20.dp, end = 20.dp)
                ) {}

                GameButton(
                    size = SettingButtonSize, onClick = { clickable.onRestart() },
                    modifier = Modifier
                        .weight(1f)
                        .padding(start = 20.dp, end = 20.dp)
                ) {}
            }
        }

        Spacer(modifier = Modifier.height(30.dp))

        val ButtonText = @Composable { modifier: Modifier, text: String ->
            Text(text = text, modifier = modifier, color = Color.White.copy(0.9f), fontSize = 18.sp)
        }

        Row(
            modifier = Modifier
                .padding(start = 40.dp, end = 40.dp)
                .height(160.dp)
        ) {
            Box(
                modifier = Modifier
                    .fillMaxHeight()
                    .weight(1f)
            ) {
                GameButton(
                    Modifier.align(TopCenter),
                    size = DirectionButtonSize,
                    onClick = { clickable.onMove(Direction.Up) },
                    autoInvokeWhenPressed = false
                ) {
                    ButtonText(it, stringResource(id = R.string.button_up))
                }
                GameButton(
                    Modifier.align(CenterStart),
                    size = DirectionButtonSize,
                    onClick = { clickable.onMove(Direction.Left) },
                    autoInvokeWhenPressed = true
                ) {
                    ButtonText(it, stringResource(id = R.string.button_left))
                }
                GameButton(
                    Modifier.align(CenterEnd),
                    size = DirectionButtonSize,
                    onClick = { clickable.onMove(Direction.Right) },
                    autoInvokeWhenPressed = true
                ) {
                    ButtonText(it, stringResource(id = R.string.button_right))
                }
                GameButton(
                    Modifier.align(BottomCenter),
                    size = DirectionButtonSize,
                    onClick = { clickable.onMove(Direction.Down) },
                    autoInvokeWhenPressed = true
                ) {
                    ButtonText(it, stringResource(id = R.string.button_down))
                }
            }
            Box(
                modifier = Modifier
                    .weight(1f)
                    .fillMaxHeight()
            ) {
                GameButton(
                    modifier = Modifier.align(CenterEnd),
                    onClick = { clickable.onRotate() },
                    autoInvokeWhenPressed = false,
                    size = RotateButtonSize
                ) {
                    ButtonText(it, stringResource(id = R.string.button_rotate))
                }
            }
        }

    }
}

fun DrawScope.drawScreenBorder(
    topLeft: Offset,
    topRight: Offset,
    bottomLeft: Offset,
    bottomRight: Offset
) {
    var path = Path().apply {
        moveTo(topLeft.x, topLeft.y)
        lineTo(topRight.x, topRight.y)
        lineTo(
            topRight.x / 2 + topLeft.x / 2,
//            topLeft.y + topRight.x / 2 + topLeft.x / 2 - topLeft.x
            topLeft.y + topRight.x / 2 - topLeft.x / 2
        )
        lineTo(
            topRight.x / 2 + topLeft.x / 2,
//            bottomLeft.y - topRight.x / 2 + topLeft.x / 2
            bottomLeft.y / 2 + topLeft.y / 2,
        )
        lineTo(bottomLeft.x, bottomLeft.y)
        close()
    }
    drawPath(path, Color.Black.copy(0.5f))

    path = Path().apply {
        moveTo(bottomRight.x, bottomRight.y)
        lineTo(bottomLeft.x, bottomLeft.y)
        lineTo(
            topRight.x / 2 + topLeft.x / 2,
//            bottomLeft.y - topRight.x / 2 + topLeft.x / 2
            bottomLeft.y / 2 + topLeft.y / 2,
        )
        lineTo(
            topRight.x / 2 + topLeft.x / 2,
//            topLeft.y + topRight.x / 2 + topLeft.x / 2 - topLeft.x
            topLeft.y + topRight.x / 2 - topLeft.x / 2
        )
        lineTo(topRight.x, topRight.y)
        close()
    }

    drawPath(path, Color.White.copy(0.5f))

}


